package leetcode_tag.树;

import leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//二叉搜索树 左子树小于根节点 右子数大于根节点
//找到根节点i 1-i-1左子树 i+1-n右子树 不改变方向(左/右) 只改变位置
// 再依次递归

public class LeetCode95 {

    public List<TreeNode> generateTrees(int n) {
        if (n == 0) {
            return new ArrayList<>();
        }
        return helper(1, n);
    }

    private List<TreeNode> helper(int start, int end) {
        List<TreeNode> result = new ArrayList<>();
        if (start > end) {
            result.add(null);
            return result;
        }
        for (int i = start; i <= end; i++) {
            List<TreeNode> lefts = helper(start, i - 1);
            List<TreeNode> rights = helper(i + 1, end);
            for (TreeNode left : lefts) {
                for (TreeNode right : rights) {
                    TreeNode tmpRoot = new TreeNode(i);
                    tmpRoot.left = left;
                    tmpRoot.right = right;
                    result.add(tmpRoot);
                }
            }
        }
        return result;
    }
//------------------------------------------------------------------
}
